//
//  ConPeronalSetViewController.m
//  Shengzhuli
//
//  Created by subook on 17/3/24.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConPeronalSetViewController.h"
#import "ConProtocolViewController.h"
#import "ConAboutUsViewController.h"
#import "ConPushMessageViewController.h"
#import <SDWebImageManager.h>
#import "UIAlertView+Entension.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "PersonNetWork.h"
#import "JPUSHService.h"

@interface ConPeronalSetViewController () <UITableViewDelegate, UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView * cacheCleanView;

@end

@implementation ConPeronalSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self createCacheCleanView];
    [self createTableView];
}
//缓存动画view
- (void) createCacheCleanView {
    
    _cacheCleanView = [[UIView alloc] initWithFrame:(CGRectMake(ScreenW/2-60, ScreenH-350, 120, 30))];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _cacheCleanView.frame.size.width, _cacheCleanView.frame.size.height)];
    
    label.text = @"缓存已清除";
    label.font = [UIFont systemFontOfSize:14];
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    label.layer.cornerRadius = 5;
    label.clipsToBounds = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [_cacheCleanView addSubview:label];
}
- (void) createTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStyleGrouped)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //_tableView.backgroundColor = RGBColorOne(234);
    
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 2;
            break;
        case 2:
            return 1;
            break;
        default:
            return 0;
            break;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 30;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    switch (indexPath.section) {
        case 0:
            if (indexPath.row == 0) {
                cell.textLabel.text = @"  清除缓存";
            }else if (indexPath.row == 1) {
                cell.textLabel.text = @"  推送消息设置";
            }else {
                cell.textLabel.text = @"  允许其他人查看名片";
                UISwitch *switchOn = [[UISwitch alloc]initWithFrame:CGRectMake(ScreenW-60,5, 50, 30)];
                switchOn.on = !self.allowStutas;
                [switchOn addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
                [cell.contentView addSubview:switchOn];
            }
            break;
        case 1:
            if (indexPath.row == 0) {
                cell.textLabel.text = @"  关于我们";
            }else {
                cell.textLabel.text = @"  用户协议";
            }
            break;
        case 2:
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.textColor = RGBColorOne(107);
            cell.textLabel.text = @"退出登录";
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 0:

            if (indexPath.row == 0) {
                [[SDWebImageManager sharedManager].imageCache clearMemory];
                [[NSURLCache sharedURLCache] removeAllCachedResponses];
                __WEAK
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.tableView addSubview:weakSelf.cacheCleanView];
                });
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.cacheCleanView removeFromSuperview];
                });
            }else if (indexPath.row == 1) {
                ConPushMessageViewController *pushVc = [[ConPushMessageViewController alloc] init];
                [self.navigationController pushViewController:pushVc animated:YES];
                
            }else {
                
            }
            break;
        case 1:
            if (indexPath.row == 0) {
                ConAboutUsViewController *aboutVc = [[ConAboutUsViewController alloc] init];
                [self.navigationController pushViewController:aboutVc animated:YES];
            }else {
                ConProtocolViewController *proVc = [[ConProtocolViewController alloc] init];
                [self.navigationController pushViewController:proVc animated:YES];
            }
            break;
        case 2: {
         
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否退出登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            __WEAK
            [alert handlerClickedButton:^(UIAlertView *alertView, NSInteger btnIndex) {
                if (btnIndex == 1) {
                    [SZLAccountHelper clearAlreadyInfo];
                    weakSelf.tabBarController.selectedIndex = 0;
                    [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                    [MBProgressHUD showSuccess:@"退出成功"];
                    [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                        NSLog(@"删除别名是否成功%zd",iResCode);
                    } seq:1000000];
                }
            }];
            [alert show];
        }
            break;
        default:
            
            break;
    }
}
- (void)switchAction:(UISwitch *)switchOn
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"allowStutas"] = @(!switchOn.on);
    [PersonNetWork allowLookCardWithParame:parame response:^(BOOL success, NSString *msg) {
        if (success) {
            [MBProgressHUD showSuccess:@"修改成功"];
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
